/**
 * Copyright (c) 2016-present, lovebing.org.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

package org.lovebing.reactnative.baidumap.support;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.facebook.react.modules.core.PermissionAwareActivity;
import com.facebook.react.modules.core.PermissionListener;

import org.lovebing.reactnative.baidumap.R;
import org.lovebing.reactnative.baidumap.constant.RequestCode;

import java.util.Date;

/**
 * @author lovebing
 * @date 2019/10/31
 */
public class AppUtils {

    public static boolean checkPermission(Activity activity, String permission,PermissionListener listener ) {

        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && activity != null) {
                if (activity.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.M) {
                    if (!hasPermission(activity, permission)) {
                        //没有权限
                        SharedPreferences data = activity.getApplicationContext().getSharedPreferences("userDataRN", Context.MODE_PRIVATE);
                        Long oldTime = data.getLong("ACCESS_FINE_LOCATION",0);
                        Boolean isShow = ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.ACCESS_FINE_LOCATION);
                        long timeGetTime = new Date().getTime()/1000;
                        //可以显示权限请求或从发起过权限请求，且距离上次请求20分钟
                        if ((isShow || (oldTime <= 1) ) && timeGetTime - oldTime >= 1200 ){
                            //没有权限且可以显示或者距离上一次显示时间超过30分钟

                            AlertDialog.Builder builder=new AlertDialog.Builder(activity.getWindow().getContext());
                            View view = View.inflate(activity, R.layout.location_dialog, null);
                            builder.setView(view);
                            builder.setCancelable(true);
                            AlertDialog al = builder.create();
                            al.setCanceledOnTouchOutside(false);//点击空白不消失
                            al.show();
                            TextView title = view.findViewById(R.id.title);
                            Button bt1 = view.findViewById(R.id.btn_cancel);
                            Button bt2 = view.findViewById(R.id.btn_comfirm);
                            bt1.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Log.i("alertll","cancel");
                                    //不给权限
                                    SharedPreferences.Editor editor = data.edit();
                                    //如果首次请求直接拒绝了，则保留权限为操作过的标志
                                    Long n_time = oldTime <= 1 ? 1: timeGetTime;
                                    editor.putLong("ACCESS_FINE_LOCATION",n_time);
                                    editor.commit();
                                    al.dismiss();
                                }
                            });
                            bt2.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    Log.i("alertll","comfirm");
                                    //可以去获取权限
                                    PermissionAwareActivity pactivit = (PermissionAwareActivity) activity;
                                    String[] ps = new String[1];
                                    ps[0] =permission;
                                    pactivit.requestPermissions( ps, 1,listener);
                                    SharedPreferences.Editor editor = data.edit();
                                    editor.putLong("ACCESS_FINE_LOCATION",timeGetTime);
                                    editor.commit();
                                    al.dismiss();
                                }
                            });


                        }

                        return  false;
                    }else {
                        //权限已经获取
                        return true;
                    }
                }
            }
            return true;
        }catch (Exception e) {
            return true;
        }
    }

    public static boolean hasPermission(Activity activity, String permission) {
        return ContextCompat.checkSelfPermission(activity, permission) == PackageManager.PERMISSION_GRANTED;
    }
}
